package org.jakim.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jakim.db.DBUtils;
import org.jakim.entity.User;
/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String userName = request.getParameter("txtUserName");
		String userPass = request.getParameter("txtUserPassword");
		User user = DBUtils.getUserFromDB(userName, userPass);
		
		if (user != null){
			HttpSession httpSeesion = request.getSession();
			httpSeesion.setAttribute("User", user);
			response.sendRedirect("userhome.jsp");
		}else
		{
			response.sendError(100, "No Such User");
		}
		//TODO: Delete code bellow
		PrintWriter out = response.getWriter();
		String str = request.getParameter("txtUserName");
		out.println("Hello, " + str);
		out.close();
	}

}
